import Reflux from 'reflux';
import PageConst from './PageConst';
import Actions from './Actions';
import update from 'react-addons-update';
import StorageUtils from '../../../app/storage';
import DB from '../../../app/db';
export default Reflux.createStore({
  listenables: [Actions],
  data: {
    ...PageConst,
    position: {},
    entity: {},
    gallery: {
      id: '',
      show: false,
    },
    warnTimer: null,
    showSuccess: false, //显示成功
    showLoading: false,//显示加载
    showWarn: false,
    dialogShow: false,//显示确认对话框
    errorMsg: '',
    files: [],
    isMy: false,
    editable: true,
  },

  onLoadMode(obj){
    let that = this;
    console.log(obj.creatorId === (StorageUtils.getUserInfo() ? StorageUtils.getUserInfo().openId : '') ? true : false);
    that.data = update(that.data, {
      isMy: {$set: obj.creatorId == (StorageUtils.getUserInfo() ? StorageUtils.getUserInfo().openId : '') ? true : false},
      entity: {$set: obj},
      editable: {$set: false},
      files: {
        $apply: function (files) {
          return obj.urlList.map((v, k)=> {
            return {url: v};
          })
        }
      },
    });
    that.updateComponent();
  },

  onEditMode(){
    let that = this;
    that.data = update(that.data, {
      entity: {
        $set: {
          location: '', // 意见发起者的地理位置
          suggesttype: 1, // 意见类型 1路灯 2景观灯 3安全文明施工 4园林绿化
          title: '', // 意见标题
          locationlat: '', // 意见发起者的地理位置纬度
          locationlng: '', // 意见发起者的地理位置经度
          viewdesc: '', // 意见详述
          faulttype: '1', // 故障类型 1故障 2损坏
          viewstatus: '1', // 意见的状态 1已提交待回复 2已回复
          datastatus: '1', // 数据状态 1正常 2已撤销
          picList: [],
          tjrNick: StorageUtils.getUserInfo() ? StorageUtils.getUserInfo().nickname : '未知',
          creatorId: StorageUtils.getUserInfo() ? StorageUtils.getUserInfo().openId : ''
        }
      },
      isMy: {$set: true},
      editable: {$set: true},
      position: {$set: {}},
      files: {$set: []},
    });
    that.updateComponent();
  },

  onTsnrChange: function (obj) {
    let that = this;
    that.data.entity = update(that.data.entity, {
      viewdesc: {$set: obj}
    });
    that.updateComponent();
  },

  onFallBackSuggestion: function (cb) {
    var that = this;
    DB.WechatAPI.fallBackSuggestion({id: that.data.entity.id}).then(
      function (content) {
        that.onHideLoading();
        that.onShowSuccess();
        setTimeout(()=> {
          that.onHideSuccess();
        }, 1500);
        cb();
      },
      function (error) {
        that.onHideLoading();
        console.log(error);
      }
    );
  },

  onYjlxChange: function (obj) {
    let that = this;
    that.data.entity = update(that.data.entity, {
      suggesttype: {$set: obj}
    });
    console.log(that.data.entity);
    that.updateComponent();
  },

  onTitleChange: function (obj) {
    let that = this;
    that.data.entity = update(that.data.entity, {
      title: {$set: obj}
    });
    that.updateComponent();
  },

  onUploadSuggestion: function (cb) {
    let that = this;

    that.data.entity = update(that.data.entity, {
      $apply: function (entity) {
        delete entity['createdTime'];
        delete entity['modifiedTime'];
        return entity;
      }
    });
    that.updateComponent();

    DB.WechatAPI.addSuggession(this.data.entity).then(
      function (content) {
        //that.onEditMode();
        that.onHideLoading();
        that.onShowSuccess();
        setTimeout(()=> {
          that.onHideSuccess();
        }, 1500);
        cb();
      },
      function (error) {
        console.log(error)
        that.onHideLoading();
      }
    );
  },

  onHideFiles: function () {
    let that = this;
    that.data.gallery = update(that.data.gallery, {show: {$set: false}});
    that.updateComponent();
  },

  onShowFiles: function (index) {
    let that = this;
    that.data.gallery = update(that.data.gallery, {
      show: {$set: true},
      id: {$set: index},
    });
    that.updateComponent();
    that.onHideLoading();
  },

  onShowWarn: function (errorMsg) {

    let that = this;
    that.data = update(that.data, {
      showWarn: {$set: true},
      errorMsg: {$set: errorMsg}
    });
    that.updateComponent();
    that.data.warnTimer = setTimeout(()=> {
      that.data = update(that.data, {
        showWarn: {$set: false},
        errorMsg: {$set: ''}
      });
      that.updateComponent();
    }, 2000);
  },

  onShowSuccess: function () {
    let that = this;
    that.data.showSuccess = update(that.data.showSuccess, {$set: true});
    that.updateComponent();
  },

  onHideSuccess: function () {
    let that = this;
    that.data.showSuccess = update(that.data.showSuccess, {$set: false});
    that.updateComponent();
  },

  onShowLoading: function () {
    let that = this;
    that.data.showLoading = update(that.data.showLoading, {$set: true});
    that.updateComponent();
  },

  onHideLoading: function () {
    let that = this;
    that.data.showLoading = update(that.data.showLoading, {$set: false});
    that.updateComponent();
  },

  onAddFiles: function (file) {
    let that = this;
    DB.WechatAPI.uploadMedia({
      filename: file.name,
      medataBase64: file.data
    }).then(
      function (content) {
        that.data = update(that.data, {
          files: {
            $push: [{url: file.data, ...content}]
          },
          entity: {
            picList: {
              $push: [].concat(content.id)
            }
          }
        });
        that.updateComponent();
        that.onHideLoading();
      },
      function (error) {
        console.log(error);
        that.showWarn(error.message);
        that.onHideLoading();
      }
    );

  },

  onDelFiles: function (id) {
    let that = this;
    that.data = update(that.data, {
      gallery: {show: {$set: false}},
      files: {
        $apply: function (files) {
          return files.filter((v, k)=>
            k != id
          );
        }
      },
      entity: {
        picList: {
          $apply: function (picList) {
            return picList.filter((v, k)=>
              k != id
            );
          }
        }
      }
    });
    that.onHideLoading();
    that.updateComponent();
  },

  onSetPosition: function (obj) {
    let that = this;
    console.log(obj);
    that.data = update(that.data, {
      position: {$set: obj},
      entity: {location: {$set: obj.name}}
    });
    that.updateComponent();
  },

  //IOS时间只支持2017/01/01 所以需要转换格式
  getDateDiff: function (startDiffTime) {
    //将xxxx-xx-xx的时间格式，转换为 xxxx/xx/xx的格式
    let startTime;
    if (startDiffTime) {
      startTime = startDiffTime.replace(/\-/g, "/");
    }

    return startTime;
  },

  onShowConfirm: function () {
    let that = this;
    that.data.dialogShow = update(that.data.dialogShow, {$set: true});
    that.updateComponent();
  },

  onHideConfirm: function () {
    let that = this;
    that.data.dialogShow = update(that.data.dialogShow, {$set: false});
    that.updateComponent();
  },

  updateComponent: function () {
    this.trigger(this.data);
  },

  getInitialState: function () {
    return this.data;
  }
});
